<?php

namespace app\common\shop;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

// 添加配置-目录
class ConfigModel extends model {

    /**
     * [添加配置]
     * @return
     */
    public static function add(){

        \app\common\shop\AddressModel::createTable();
        \app\common\shop\CartModel::createTable();
        \app\common\shop\OrderGoodsModel::createTable();
        \app\common\shop\OrderModel::createTable();
        \app\common\shop\ShopGoodsModel::createTable();
        \app\common\shop\SpecListModel::createTable();
        \app\common\shop\SpecModel::createTable();
        \app\common\shop\UserTeamLevelModel::createTable();
        \app\common\shop\UserAgentLevelModel::createTable();
        \app\common\shop\UserAreaLevelModel::createTable();
        \app\common\shop\UserHouseLevelModel::createTable();
        \app\common\shop\SignInLogModel::createTable();
        \app\common\shop\HomeCateModel::createTable();
        \app\common\shop\ShopGroupModel::createTable();
        \app\common\shop\WithdrawLogModel::createTable();
        \app\common\shop\RealNameModel::createTable();
        \app\common\shop\UserApplyLogModel::createTable();
        \app\common\shop\ArticleCateModel::createTable();
        \app\common\shop\RechargeLogModel::createTable();
        self::mywallet();
        self::menu();
        self::insertUserTeamLevel();
        self::insertUserAgentLevel();
        self::insertUserHouseLevel();
        self::insertUserAreaLevel();
        self::user();
        self::config();
        self::goods();
        self::order();
        self::article();
    }

    /**
     * [配置]
     * @param
     * @return
     */
    public static function config(){
        $shop_return_one = Db::table('config')->where('key','shop_return_one')->find();
        if(empty($shop_return_one)){
            Db::table('config')->insertGetId(['name'=>'一级购物返(%)','key'=>'shop_return_one','value'=>1,'type'=>2,'sort'=>1,'status'=>1]);
        }

        $shop_return_two = Db::table('config')->where('key','shop_return_two')->find();
        if(empty($shop_return_two)){
            Db::table('config')->insertGetId(['name'=>'二级购物返(%)','key'=>'shop_return_two','value'=>1,'type'=>2,'sort'=>1,'status'=>1]);
        }

        $shop_return_three = Db::table('config')->where('key','shop_return_three')->find();
        if(empty($shop_return_three)){
            Db::table('config')->insertGetId(['name'=>'三级购物返(%)','key'=>'shop_return_three','value'=>1,'type'=>2,'sort'=>1,'status'=>1]);
        }
        $register_recommend_bless = Db::table('config')->where('key','register_recommend_bless')->find();
        if(empty($register_recommend_bless)){
            Db::table('config')->insertGetId(['name'=>'推荐注册赠送福币','key'=>'register_recommend_bless','value'=>1,'type'=>2,'sort'=>1,'status'=>1]);
        }
        $sign_in_bless = Db::table('config')->where('key','sign_in_bless')->find();
        if(empty($sign_in_bless)){
            Db::table('config')->insertGetId(['name'=>'签到赠送福币','key'=>'sign_in_bless','value'=>1,'type'=>2,'sort'=>1,'status'=>1]);
        }
        $shop_group_time = Db::table('config')->where('key','shop_group_time')->find();
        if(empty($shop_group_time)){
            Db::table('config')->insertGetId(['name'=>'拼团时间（分）','key'=>'shop_group_time','value'=>1,'type'=>2,'sort'=>1,'status'=>1]);
        }
        $withdrawal_time = Db::table('config')->where('key','withdrawal_time')->find();
        if(empty($withdrawal_time)){
            Db::table('config')->insertGetId(['name'=>'提现日（每个月几号,大于31则以31为准）','key'=>'withdrawal_time','value'=>1,'type'=>2,'sort'=>1,'status'=>1]);
        }
        $withdrawal_fee = Db::table('config')->where('key','withdrawal_fee')->find();
        if(empty($withdrawal_fee)){
            Db::table('config')->insertGetId(['name'=>'提现手续费（%）','key'=>'withdrawal_fee','value'=>1,'type'=>2,'sort'=>1,'status'=>1]);
        }

        $agent_time = Db::table('config')->where('key','agent_time')->find();
        if(empty($agent_time)){
            Db::table('config')->insertGetId(['name'=>'代理商有效时间（天）','key'=>'agent_time','value'=>1,'type'=>2,'sort'=>1,'status'=>1]);
        }

        $all_money_order = Db::table('config')->where('key','all_money_order')->find();
        if(empty($all_money_order)){
            Db::table('config')->insertGetId(['name'=>'总营业额','key'=>'all_money_order','value'=>1,'type'=>2,'sort'=>1,'status'=>1]);
        }
    }
    /**
     * [表是否存在字段]
     * @return
     */
    public static function describe($table,$field){
        $slq = 'describe `'. $table.'` `'.$field.'`';
        $res = Db::query($slq);
        if($res) return true;
        return false;
    }

    /**
     * [添加 my_wallet表字段]
     * @return
     */
    public static function mywallet(){
//        shop_spec_list
        if(!self::describe('shop_spec_list','team_prices')){
            Db::query("ALTER TABLE `shop_spec_list` ADD `team_prices` DECIMAL(22,2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '拼团价'");
        }
        if(!self::describe('my_wallet','bless')){
            Db::query("ALTER TABLE `my_wallet` ADD `bless` DECIMAL(22,2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '福币余额' AFTER `lock`");
        }
        if(!self::describe('my_wallet','coupon')){
            Db::query("ALTER TABLE `my_wallet` ADD `coupon` DECIMAL(22,2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '代金券余额' AFTER `lock`");
        }
        if(!self::describe('my_wallet','focas')){
            Db::query("ALTER TABLE `my_wallet` ADD `focas` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '福卡' AFTER `lock`");
        }
        if(!self::describe('my_wallet','money')){
            Db::query("ALTER TABLE `my_wallet` ADD `money` DECIMAL(22,2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '余额' AFTER `lock`");
        }
        if(!self::describe('my_wallet','credit')){
            Db::query("ALTER TABLE `my_wallet` ADD `credit` DECIMAL(22,2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '积分' AFTER `lock`");
        }
        return true;
    }

    /**
     * [添加目录]
     * @param
     * @return
     */
    public static function menu(){
        $sfsz = Db::table('manage_menu')->where('path','身份设置')->find();
        if(empty($sfsz)){
            $mmid = Db::table('manage_menu')->insertGetId(['path'=>'身份设置','level'=>1,'sort'=>8,'parent_id'=>0]);
            $m2 = Db::table('manage_menu')->insertGetId(['path'=>'身份设置|团长设置','level'=>2,'sort'=>1,'parent_id'=>$mmid]);
            Db::table('manage_power')->insertGetId(['path'=>'/admin/userlevel/team','method'=>'GET','url'=>'','menu_id'=>$m2]);
            $m3 = Db::table('manage_menu')->insertGetId(['path'=>'身份设置|代理商','level'=>2,'sort'=>2,'parent_id'=>$mmid]);
            Db::table('manage_power')->insertGetId(['path'=>'/admin/userlevel/agent','method'=>'GET','url'=>'','menu_id'=>$m3]);
            $m3 = Db::table('manage_menu')->insertGetId(['path'=>'身份设置|房主','level'=>2,'sort'=>3,'parent_id'=>$mmid]);
            Db::table('manage_power')->insertGetId(['path'=>'/admin/userlevel/house','method'=>'GET','url'=>'','menu_id'=>$m3]);
            $m3 = Db::table('manage_menu')->insertGetId(['path'=>'身份设置|区域代理','level'=>2,'sort'=>4,'parent_id'=>$mmid]);
            Db::table('manage_power')->insertGetId(['path'=>'/admin/userlevel/agency','method'=>'GET','url'=>'','menu_id'=>$m3]);
        }

        $scsy = Db::table('manage_menu')->where('path','商城管理|首页设置')->where('parent_id',201)->find();
        if(empty($scsy)){
            $m4 = Db::table('manage_menu')->insertGetId(['path'=>'商城管理|首页设置','level'=>2,'sort'=>7,'parent_id'=>201]);
            Db::table('manage_power')->insertGetId(['path'=>'/admin/shopgoods/cate','method'=>'GET','url'=>'','menu_id'=>$m4]);
        }

        $wzfl = Db::table('manage_menu')->where('path','内容管理|文章分类')->where('parent_id',54)->find();
        if(empty($wzfl)){
            $m4 = Db::table('manage_menu')->insertGetId(['path'=>'内容管理|文章分类','level'=>2,'sort'=>7,'parent_id'=>54]);
            Db::table('manage_power')->insertGetId(['path'=>'/admin/article/cate','method'=>'GET','url'=>'','menu_id'=>$m4]);
        }

        $wzfl = Db::table('manage_menu')->where('path','内容管理|联系客服')->where('parent_id',54)->find();
        if(empty($wzfl)){
            $m4 = Db::table('manage_menu')->insertGetId(['path'=>'内容管理|联系客服','level'=>2,'sort'=>7,'parent_id'=>54]);
            Db::table('manage_power')->insertGetId(['path'=>'/admin/article/person','method'=>'GET','url'=>'','menu_id'=>$m4]);
        }

//        内容管理|帮助中心
        $bzzx = Db::table('manage_menu')->where('path','内容管理|帮助中心')->where('parent_id',54)->where('level',2)->find();
        if($bzzx){
            Db::table('manage_menu')->where('id',$bzzx['id'])->setField('level',0);
        }
    }

    /**
     * [添加团长信息]
     * @param
     * @return
     */
    public static function insertUserTeamLevel(){
        $info = Db::table('user_team_level')->find();
        if(!$info){

            Db::table('user_team_level')->insertGetId(['name'=>'V1','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','peers'=>0,'createtime'=>time()]);
            Db::table('user_team_level')->insertGetId(['name'=>'V2','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','peers'=>0,'createtime'=>time()]);
            Db::table('user_team_level')->insertGetId(['name'=>'V3','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','peers'=>0,'createtime'=>time()]);
            Db::table('user_team_level')->insertGetId(['name'=>'V4','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','peers'=>0,'createtime'=>time()]);
            Db::table('user_team_level')->insertGetId(['name'=>'V5','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','peers'=>0,'createtime'=>time()]);
            Db::table('user_team_level')->insertGetId(['name'=>'V6','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','peers'=>0,'createtime'=>time()]);
        }
    }

    /**
     * [添加代理商信息]
     * @param
     * @return
     */
    public static function insertUserAgentLevel(){
        $info = Db::table('user_agent_level')->find();
        if(!$info){

            Db::table('user_agent_level')->insertGetId(['name'=>'VIP1','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','peers'=>0,'createtime'=>time()]);
            Db::table('user_agent_level')->insertGetId(['name'=>'VIP2','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','peers'=>0,'createtime'=>time()]);
            Db::table('user_agent_level')->insertGetId(['name'=>'VIP3','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','peers'=>0,'createtime'=>time()]);
            Db::table('user_agent_level')->insertGetId(['name'=>'VIP4','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','peers'=>0,'createtime'=>time()]);
            Db::table('user_agent_level')->insertGetId(['name'=>'VIP5','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','peers'=>0,'createtime'=>time()]);
            Db::table('user_agent_level')->insertGetId(['name'=>'VIP6','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','peers'=>0,'createtime'=>time()]);
        }
        if(!self::describe('user_agent_level','fenhong')){
            Db::query("ALTER TABLE `user_agent_level` ADD `fenhong` decimal(22,2) NOT NULL DEFAULT '0' COMMENT '分红比例'");
        }

    }

    /**
     * [添加房主信息]
     * @param
     * @return
     */
    public static function insertUserHouseLevel(){
        $info = Db::table('user_house_level')->find();
        if(!$info){
            Db::table('user_house_level')->insertGetId(['name'=>'小房主','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','createtime'=>time()]);
            Db::table('user_house_level')->insertGetId(['name'=>'中房主','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','createtime'=>time()]);
            Db::table('user_house_level')->insertGetId(['name'=>'大房主','direct'=>'5','team'=>'20','selfperformance'=>0,'teamperformance'=>0,'percentage'=>'2','createtime'=>time()]);
        }
        if(!self::describe('user_house_level','peers')){
            Db::query("ALTER TABLE `user_house_level` ADD `peers` decimal(22,2) NOT NULL DEFAULT '0' COMMENT '分红比例'");
        }
    }
    /**
     * [添加区域代理信息]
     * @param
     * @return
     */
    public static function insertUserAreaLevel(){
        $info = Db::table('user_area_level')->find();
        if(!$info){
            // alliance 加盟条件	selfpercentage 直推佣金提成	percentage 佣金提成	createtime 时间

            Db::table('user_area_level')->insertGetId(['name'=>'市代理','alliance'=>'5','selfpercentage'=>'20','percentage'=>0,'createtime'=>time()]);
            Db::table('user_area_level')->insertGetId(['name'=>'区代理','alliance'=>'5','selfpercentage'=>'20','percentage'=>0,'createtime'=>time()]);
        }

        if(!self::describe('user_area_level','peers')){
            Db::query("ALTER TABLE `user_area_level` ADD `peers` decimal(22,2) NOT NULL DEFAULT '0' COMMENT '分红比例'");
        }
    }

    /**
     * [会员表]
     * @param
     * @return
     */
    public static function user(){
        if(!self::describe('user','team_level')){
            Db::query("ALTER TABLE `user` ADD `team_level` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '团长等级'");
        }
        if(!self::describe('user','agent_level')){
            Db::query("ALTER TABLE `user` ADD `agent_level` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '代理等级'");
        }
        if(!self::describe('user','house_level')){
            Db::query("ALTER TABLE `user` ADD `house_level` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '房主等级'");
        }
        if(!self::describe('user','area_level')){
            Db::query("ALTER TABLE `user` ADD `area_level` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '区域代理等级'");
        }

        if(!self::describe('user','selfperformance')){
            Db::query("ALTER TABLE `user` ADD `selfperformance` DECIMAL(22,2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '个人业绩'");
        }
        if(!self::describe('user','teamperformance')){
            Db::query("ALTER TABLE `user` ADD `teamperformance` DECIMAL(22,2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '团队总业绩'");
        }
        if(!self::describe('user','mathteamperformance')){
            Db::query("ALTER TABLE `user` ADD `mathteamperformance` DECIMAL(22,2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '本月团队总业绩'");
        }
        if(!self::describe('user','mathteamperformancetime')){
            Db::query("ALTER TABLE `user` ADD `mathteamperformancetime` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '本月计算时间（用于判断本月是否已经开始计算）'");
        }
        if(!self::describe('user','mathteamperformancetime')){
            Db::query("ALTER TABLE `user` ADD `mathteamperformancetime` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '本月计算时间（用于判断本月是否已经开始计算）'");
        }
        if(!self::describe('user','signbless')){
            Db::query("ALTER TABLE `user` ADD `signbless` DECIMAL(22,2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '签到累计福币'");
        }
        if(!self::describe('user','province')){
            Db::query("ALTER TABLE `user` ADD `province` varchar(255) NULL DEFAULT NULL COMMENT '区域代理地区-省'");
        }
        if(!self::describe('user','city')){
            Db::query("ALTER TABLE `user` ADD `city` varchar(255) NULL DEFAULT NULL COMMENT '区域代理地区-市'");
        }
        if(!self::describe('user','area')){
            Db::query("ALTER TABLE `user` ADD `area` varchar(255) NULL DEFAULT NULL COMMENT '区域代理地区'");
        }
        if(!self::describe('user','performance')){
            Db::query("ALTER TABLE `user` ADD `performance` decimal(22,2) not NULL DEFAULT 0 COMMENT '本月团队总业绩'");
        }
        if(!self::describe('user','house_over_time')){
            Db::query("ALTER TABLE `user` ADD `house_over_time` varchar(255) not NULL DEFAULT 0 COMMENT 'shijian'");
        }
        if(!self::describe('user','area_over_time')){
            Db::query("ALTER TABLE `user` ADD `area_over_time` varchar(255) not NULL DEFAULT 0 COMMENT 'shijian'");
        }
        if(!self::describe('user','partner_over_time')){
            Db::query("ALTER TABLE `user` ADD `partner_over_time` varchar(255) not NULL DEFAULT 0 COMMENT 'shijian'");
        }
        // openid
        if(!self::describe('user','openid')){
            Db::query("ALTER TABLE `user` ADD `openid` varchar(255) NULL COMMENT '微信openid' AFTER `id`");
        }
    }

    /**
     * [商品表]
     * @param
     * @return
     */
    public static function goods(){

        if(!self::describe('shop_goods','homecate')){
            Db::query("ALTER TABLE `shop_goods` ADD `homecate` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '首页分类id'");
        }
        if(!self::describe('shop_goods','groupnumber')){
            Db::query("ALTER TABLE `shop_goods` ADD `groupnumber` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '团购人数'");
        }
        if(!self::describe('shop_goods','credit')){
            Db::query("ALTER TABLE `shop_goods` ADD `credit` decimal(22,2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '赠送积分'");
        }
        // goods_category 分类归属
        if(!self::describe('goods_category','shoptype')){
            Db::query("ALTER TABLE `goods_category` ADD `shoptype` int(11) UNSIGNED NOT NULL DEFAULT '1' COMMENT '分类归属'");
        }
    }

    /**
     * [订单表]
     * @param
     * @return
     */
    public static function order(){
        if(!self::describe('shop_order','groupid')){
            Db::query("ALTER TABLE `shop_order` ADD `groupid` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '拼团id'");
        }
        if(!self::describe('shop_order','groupstatus')){
            Db::query("ALTER TABLE `shop_order` ADD `groupstatus` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '拼团状态 0拼团中 1拼团成功'");
        }
        if(!self::describe('shop_order','isshare')){
            Db::query("ALTER TABLE `shop_order` ADD `isshare` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '是否计算过分红 0否 1是'");
        }
        if(!self::describe('shop_order','credit')){
            Db::query("ALTER TABLE `shop_order` ADD `credit` decimal(22,2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '赠送积分'");
        }
        if(!self::describe('shop_order','isgroup')){
            Db::query("ALTER TABLE `shop_order` ADD `isgroup` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '是否拼团单独购买 0是 1否'");
        }
        if(!self::describe('shop_order','overcommssion')){
            Db::query("ALTER TABLE `shop_order` ADD `overcommssion` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '是否已经计算过奖励 0是 1否'");
        }


    }
    /**
     * [文章表]
     * @param
     * @return
     */
    public static function article(){
        if(!self::describe('article','cateid')){
            Db::query("ALTER TABLE `article` ADD `cateid` int(11) UNSIGNED NOT NULL DEFAULT '0' COMMENT '分类id'");
        }
    }
}